package Editor 
{
	import fl.controls.Button;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class RegionsEditor extends Sprite 
	{
		private var FContainer:Sprite;
		private var FNewRegion:Button;
		private var FGetXML:Button;
		private var FRegions:Vector.<RegionPoint>;
		private var FCurrentPoint:RegionPoint;
		
		public function RegionsEditor() 
		{
			addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
			addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);		
			FNewRegion = new Button();
			FNewRegion.label = 'Разметка нового учатска';
			FNewRegion.width = FNewRegion.textField.textWidth + 20;
			FNewRegion.useHandCursor = FNewRegion.buttonMode = true;
			FNewRegion.addEventListener(MouseEvent.MOUSE_DOWN, OnNewRegion);
			FGetXML = new Button();
			FGetXML.label = 'XML-код участка';
			FGetXML.width = FGetXML.textField.textWidth + 20;
			FGetXML.useHandCursor = FGetXML.buttonMode = true;
			FGetXML.addEventListener(MouseEvent.MOUSE_DOWN, OnXMLCode);
			FContainer = new Sprite();
			addChild(FContainer);
			addChild(FNewRegion);
			addChild(FGetXML);
			FRegions = new Vector.<RegionPoint>;
		}
		
		private function OnXMLCode(e:MouseEvent):void 
		{
			var vResult:String = new String();
			if (FRegions.length >= 3)
			{
				for (var i in FRegions)
				{
					if (i > 0)		
						vResult += ',';
					vResult += Math.round(FRegions[i].x) + ':' + Math.round(FRegions[i].y);
				}
				vResult += ',' + Math.round(FRegions[0].x) + ':' + Math.round(FRegions[0].y);
				trace(vResult);
			}			
		}
		
		private function OnEnterFrame(e:Event):void 
		{
			if (FCurrentPoint)
			{
				FCurrentPoint.x = mouseX;
				FCurrentPoint.y = mouseY;
			}
		}
		
		private function OnNewRegion(e:MouseEvent):void 
		{
			for (var i in FRegions)			
				FContainer.removeChild(FRegions[i]);
			FRegions = new Vector.<RegionPoint>;
			FContainer.graphics.clear();
		}
		
		private function OnMouseDown(e:MouseEvent):void 
		{
			if (e.target == this)
			{
				AddPoint(e.stageX, e.stageY);
				if (FRegions.length >= 3)				
					Redraw();	
			}
		}
		
		private function Redraw():void
		{
			FContainer.graphics.clear();
			FContainer.graphics.beginFill(0x1CC62D, 0.5);
			FContainer.graphics.lineStyle(1, 0, 0);
			FContainer.graphics.moveTo(FRegions[0].x, FRegions[0].y);			
			for (var i:int = 1; i < FRegions.length; i++)			
				FContainer.graphics.lineTo(FRegions[i].x, FRegions[i].y);
			FContainer.graphics.lineTo(FRegions[0].x, FRegions[0].y);
			FContainer.graphics.endFill();
		}
		
		private function AddPoint(AX:int, AY:int):RegionPoint
		{
			var vResult:RegionPoint = new RegionPoint();			
			FContainer.addChild(vResult);
			vResult.x = AX;
			vResult.y = AY;
			FRegions.push(vResult);
			vResult.addEventListener(MouseEvent.MOUSE_DOWN, OnPointClick);
			vResult.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown)
			return vResult;
		}
		
		private function OnKeyDown(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.DELETE)
			{
				var vIndex:int = FRegions.indexOf(e.target);
				if (vIndex > -1)
				{
					trace('Deleting point ' + vIndex);
					FRegions.splice(vIndex, 1);
					if (RegionPoint(e.target).parent)
					{
						FContainer.removeChild(RegionPoint(e.target));
					}
					if(FRegions.length >= 1)
						Redraw();
				}
			}
		}
		
		private function OnPointClick(e:MouseEvent):void 
		{
			FCurrentPoint = RegionPoint(e.target);
		}
		
		private function OnAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
			stage.addEventListener(Event.RESIZE, OnStageResize);
			stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			OnStageResize(null);
		}
		
		private function OnMouseUp(e:MouseEvent):void 
		{
			if (FCurrentPoint)
				stage.focus = FCurrentPoint;
			FCurrentPoint = null;
			if(FRegions.length >= 1)
				Redraw();
		}
		
		private function OnStageResize(e:Event):void 
		{
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			FNewRegion.x = stage.stageWidth - FNewRegion.width - 10;
			FNewRegion.y = stage.stageHeight - FNewRegion.height - 10;
			FGetXML.x = FNewRegion.x - FGetXML.width - 10;
			FGetXML.y = FNewRegion.y;
		}
	}
}